package com.tuanzi.loan.web.bean.receipt.product;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.receipt.product.ReceiptProductCondition;
import com.tuanzi.loan.business.entity.receipt.product.ReceiptProductInfo;
import com.tuanzi.loan.business.entity.system.Dictionary;
import com.tuanzi.loan.business.enums.RegionTreeEnum;
import com.tuanzi.loan.business.service.common.CommRegionNewService;
import com.tuanzi.loan.business.service.receipt.product.ReceiptProductConditionService;
import com.tuanzi.loan.business.service.receipt.product.ReceiptProductInfoService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import com.tuanzi.server.loan_base.constant.receipt.LoanReceiptProductComboType;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.tuanzi.loan.business.constants.DictionaryCode.LOAN_RECEIPT_PRODUCT_ICON;
import static com.tuanzi.loan.business.constants.DictionaryCode.RECEIPT_SUBACCOUNT_ORDER_ALLOT;
import static com.tuanzi.loan.business.constants.DictionaryCode.WEEKS;


@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class ReceiptProductManagerBean extends JSFInfoBean<ReceiptProductInfo> {

	private Dictionary iconDictionary;
	private Long productId;
	private boolean editor =true;
	private ReceiptProductCondition receiptProductCondition;
	private TreeNode areaRegion;
	private TreeNode[] selectedRegionNodes;

	@Autowired
	private ReceiptProductInfoService receiptProductInfoService;
	@Autowired
	private ReceiptProductConditionService receiptProductConditionService;
	@Autowired
	private CommRegionNewService commRegionNewService;

	@PostConstruct
	private void init(){

		String id = getParamFromRequest("id");
		if(StringUtils.isNotBlank(id)){
			productId = Long.parseLong(id);
			$ = receiptProductInfoService.findOne(productId);
			receiptProductCondition = receiptProductConditionService.findReceiptProductConditionByProductInfo($);
		}

		if(Objects.isNull($)){
			addWarnMessage("没有找到对应的产品配置信息");
			return;
		}

		if(Objects.isNull(receiptProductCondition)){
			receiptProductCondition = new ReceiptProductCondition();
		}

		initIcon($.getIcon());
		initRegion(receiptProductCondition.getProvince(),receiptProductCondition.getCity());
	}


	public void initRegion(String province, String city){
		if(StringUtils.isBlank(province)){
			province = "";
		}
		if(StringUtils.isBlank(city)){
			city = "";
		}

		areaRegion = commRegionNewService.generateTreeNode(province+city, RegionTreeEnum.RECEIPT);
	}

	/**
	 * 初始换icon列表
	 *
	 * @param icon
	 */
	private void initIcon(String icon) {
		if (StringUtils.isBlank(icon)) {
			return;
		}

		List<Dictionary> iconDictionaries = getDictionaries().get(DictionaryCode.LOAN_RECEIPT_PRODUCT_ICON);
		if (CollectionUtils.isNotEmpty(iconDictionaries)) {
			iconDictionary = iconDictionaries.stream().filter(x -> StringUtils.equals(x.getValue1(), icon)).findFirst().get();
		}
	}


	public void showEditor(){
		editor = true;
	}

	public void notShowEditor(){
		editor = false;
	}

	public void onCancel(){
		showEditor();
		$ = receiptProductInfoService.findOne(productId);
		receiptProductCondition = receiptProductConditionService.findReceiptProductConditionByProductId(productId);
	}

	public void onSave(){
		$.setIcon(null == iconDictionary ? null : iconDictionary.getValue1());
		receiptProductInfoService.saveOrUpdate($);
		receiptProductCondition.setProductId($.getId());
		Map<String, String> map = commRegionNewService.treeNodesToString(selectedRegionNodes);
		receiptProductCondition.setProvince(map.get("province"));
		receiptProductCondition.setCity(map.get("city"));
		receiptProductConditionService.saveOrUpdate(receiptProductCondition);
		showEditor();
		$ = receiptProductInfoService.findOne(productId);
		receiptProductCondition = receiptProductConditionService.findReceiptProductConditionByProductId(productId);
		addSuccessfullyMessage();
	}

	public void uploadLogo(FileUploadEvent event) {
		String url = this.uploadImg(event.getFile());
		$.setLogo(url);
		addSuccessfullyMessage();
	}

	public void comboChange(){
		Integer comboType = $.getComboType();
		$.setOperationPermission(true);
		$.setSesameAttestation(true);
		if(comboType == LoanReceiptProductComboType.USER_PHONERECORD_PERMISSION.getType()){
			$.setUnitPrice(new BigDecimal(13));
		}
		if(comboType == LoanReceiptProductComboType.USER_PERMISSION.getType()){
			$.setOperationPermission(true);
			$.setSesameAttestation(false);
			$.setUnitPrice(null);
		}
		if(comboType == LoanReceiptProductComboType.USER_PHONERECORE_SESAME_PERMISSION.getType()){
			$.setUnitPrice(new BigDecimal(15));
		}
	}

	public boolean showUnitPrice(){
		if(!editor){
			if($.isComboTwo()){
				return true;
			}
			return false;
		}

		return false;
	}


	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(LOAN_RECEIPT_PRODUCT_ICON,WEEKS, RECEIPT_SUBACCOUNT_ORDER_ALLOT);
	}
}
